(function($,window,document,undefined){


       var pluginName = 'ajaxFormSubmitCodeigniter',
           data= { message : '' },
           s = 'success',
           defaults = {
                url:!1,
                containerMessage:".box-message",
                containerLoadingMessage:"",
                animateBody:true,
                animateContainer:!1,
                bootstrapMessageClass:'alert'

           };



       function Plugin(element,options){

            this.element = element;
            this.options = $.extend({},defaults,options);
            this._name = pluginName;
            this._defaults = defaults;
            this.init();

       }


       Plugin.prototype = {

           init:function(){
            
                 var that= this;

                 $(this.element).on('submit',function(ev){

                      ev.preventDefault();
                      
                     var $ajax = that.fsubmit();

                     $ajax.then(function(data){
                              var s = 'danger', message = '';
                              if(data.error  == false){
                                   s = 'success';
                                   that.resetFormData();
                              }
                              
                            var  
                              
                              markup = '<div class="'+that.options.bootstrapMessageClass+' '+that.options.bootstrapMessageClass+'-'+s+' imessage-ajax-form">'+data.message+'</div>';
                              $(that.options.containerMessage).html(markup);
                              
                              if(that.options.animateBody){
                                  $('body').animate({
                                   scrollTop:120
                                 },1000);  
                              }  
                              
                              if(that.options.animateContainer && data.error == true){

                                       var ele =  $(that.options.animateContainer).offset(),
                                           posLeft = $(that.options.animateContainer).offset().left + ($(that.options.animateContainer).outerWidth()/2 ); 
                                       $(that.options.animateContainer).animate({'left': (posLeft + 40 ) +'px'}, 50, 
                                              function(){$(this).animate({left : posLeft  + 'px'}, 50,
                                                 function(){
                                                          $(this).animate({left : (posLeft - 40) + 'px'}, 50,
                                                           function(){
                                                                 $(this).animate({left : posLeft  + 'px'}, 50);

                                                        });
                                                });
                                              });  
                              }
                              $('.imessage-ajax-form').show();
                              if(that.options.url && data.error == false){
                                window.location = that.options.url; 
                              }

                     }).done(function(){

                        $('.message-ajax-codeigniter').hide();
                     });
                 });


           },

           fsubmit:function(){
              var that = this; 
              return $.ajax({
                    url:$(this.element).attr('action')+'_ajax',
                    type:'post',
                    dataType:'json',
                    beforeSend:function(){
                         var mkp = '<div class="row message-ajax-codeigniter"><div class="col-md-2 col-md-offset-10">Aguarde..</div></div>';
                         
                         if(that.options.containerLoadingMessage == ""){
                           $(that.element).find('.message-ajax-codeigniter').remove();  
                           $(that.element).append(mkp);   
                         }

                         if(that.options.containerLoadingMessage == ""){
                              $(that.options.containerLoadingMessage).find('.message-ajax-codeigniter').remove();  
                              $(that.options.containerLoadingMessage).append(mkp);
                         }
                         $('.imessage-ajax-form').hide();
                        
                    },
                    data:this.getFormData()
              });

           },

           getFormData: function(){
              var dataForm = {};
              $(this.element).find('input[type="text"],input[type="password"],input[type="hidden"],textarea,select,input[type="checkbox"],input[type="radio"]').each(function(){
                    var _name = $(this).attr('name'), _type = $(this).attr('type');    
                    if(_name && (_type != 'radio' || _type != 'checkbox')){
                        dataForm[_name] = this.value;    
                    }
                    if(_type == 'radio' || _type == 'checkbox'){
                        dataForm[_name] = '';    
                    }
                          

              });
              $(this.element).find(':checked').each(function(){
                  var _name = $(this).attr('name'), is_array_e = /\[\]/gi;    
                    if(_name){

                          if(is_array_e.test(_name)){
                                       
                                       if(!dataForm[_name] || typeof dataForm[_name] == 'Object' ){
                                          dataForm[_name] = [];
                                       }
                                    
                                       dataForm[_name].push(this.value);    
                          }else{
                            dataForm[_name] = this.value; 
                          }
                       
                    }
              });
              return dataForm;

           },
           resetFormData:function(){
                  $(this.element).find('input[type="text"],input[type="password"],input[type="hidden"],textarea,select,input[type="checkbox"],input[type="radio"]').each(function(){
                    var _name = $(this).attr('name'), _type = $(this).attr('type');    
                    if(_name && (_type != 'radio' || _type != 'checkbox')){
                         this.value = '';    
                    }
                          

              });
              $(this.element).find(':checked').each(function(){
                  var _name = $(this).attr('name'), is_array_e = /\[\]/gi;    
                    if(_name){

                       $(this).attr('checked',false);                           
                    }
              });

           }


                };


       $.fn[pluginName]= function(options){
        return this.each(function(){
            if(!$.data(this , 'plugin_'+pluginName)){
                $.data(this,'plugin_'+pluginName , new Plugin(this, options));   
            }
        })
    }

})(jQuery,window,document);